// 分类模型
const { Collect } = require('../../../models/collect');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

module.exports = async (req, res) => {
	// 商品id
	let { user_id,goods_id,is_collect } = req.fields;
	console.log(req.fields);
	
	let collect = await Collect.findOne({user_id,goods_id});
	// 没有收藏
	if (!collect) {
		collect = new Collect(req.fields);
		// 保存收藏信息
		collect.save()
		.then(()=>{
			DataReturn.getData(res,200,[],'收藏成功')
		}).catch(()=>{
			DataReturn.getData(res,400,[],'收藏失败')
		})
	} else {
		// 已收藏
		Collect.updateOne({user_id,goods_id},{is_collect})
		.then(()=>{
			let text;
			is_collect ? (text='收藏成功') : (text='取消收藏');
			DataReturn.getData(res,200,[],text)
		}).catch(()=>{
			DataReturn.getData(res,400,[],'收藏失败')
		})
	}

	
}